package com.lin.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.mapper.FormMapper;
import com.lin.mapper.Plus.FormMapperPlus;
import com.lin.mapper.UserMapper;
import com.lin.pojo.Form;
import com.lin.pojo.user;
import com.lin.response.CommonResp;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.Math.max;

@Service
public class FormService extends ServiceImpl<FormMapperPlus, Form> {
    @Autowired
    private FormMapper formMapper;

    @Autowired
    private UserMapper userMapper;

    public CommonResp page(Integer pageNum,Integer pageSize,String username){
        pageNum=(pageNum-1)*pageSize;
        Map<String,Object> res = new HashMap<>();
        List<Form> data = formMapper.SelectPage(pageNum,pageSize,username);
        Integer total = formMapper.SelectCount(username);
        res.put("data",data);
        res.put("total",total);
        CommonResp< Map<String,Object> > commonResp = new CommonResp();
        commonResp.setContent(res);
        commonResp.setSuccess(true);
        commonResp.setMessage("查找成功。");
        return commonResp;
    }

    public CommonResp page_All(Integer pageNum,Integer pageSize){
        pageNum=(pageNum-1)*pageSize;
        List<Form> data = formMapper.SelectPage_All(pageNum,pageSize);
        Integer total = formMapper.SelectCount_All();
        Map<String,Object> res = new HashMap<>();
        res.put("data",data);
        res.put("total",total);
        CommonResp< Map<String,Object> > commonResp = new CommonResp();
        commonResp.setContent(res);
        commonResp.setSuccess(true);
        commonResp.setMessage("查找成功。");
        return commonResp;
    }
    public CommonResp cancel(Form form){
        Integer formid = form.getFormid();
        Integer flag = formMapper.Delete(formid);
        CommonResp<String> commonResp = new CommonResp<>();
        if(flag > 0 ){
            String username = form.getUsername();
            Integer money = userMapper.Select_ByName(username);
            Integer cur_Money = form.getValue()+money;
            userMapper.UpdateMoney(username,cur_Money);
            commonResp.setSuccess(true);
            commonResp.setMessage("取消成功。");
            commonResp.setContent(null);
        }
        else{
            commonResp.setSuccess(false);
            commonResp.setMessage("取消失败，该订单正在准备，请耐心等待。");
            commonResp.setContent(null);
        }
        return commonResp;
    }

    public CommonResp transform(Form form,Integer type){
        Integer formid = form.getFormid();
        CommonResp<String> commonResp = new CommonResp<>();
        if(type == 0 ){                     //点击“开始”，开始准备此订单
            Integer flag = formMapper.Update_Begin(formid);
            if(flag > 0 ){
                commonResp.setSuccess(true);
                commonResp.setMessage("修改订单状态成功。");
                commonResp.setContent(null);
            }
            else{
                commonResp.setSuccess(false);
                commonResp.setMessage("修改订单状态失败。");
                commonResp.setContent(null);
            }
        }
        else{                           //点击"完成"，完成此订单
            formMapper.Update_totalTime(formid);
            Integer flag = formMapper.Update_End(formid);
            if(flag > 0 ){
                commonResp.setSuccess(true);
                commonResp.setMessage("成功完成该订单。");
                commonResp.setContent(null);
            }
            else{
                commonResp.setSuccess(false);
                commonResp.setMessage("完成订单失败。");
                commonResp.setContent(null);
            }
        }
        return commonResp;
    }

    /*********************** 管理端 *********************************/
    public CommonResp echarts (){
        Integer count = formMapper.SelectCount_TI();
        System.out.println(max(count-7,0));
        Double avg = formMapper.SelectAvg();
        Integer Income = formMapper.SelectIncome();
        List<Integer> data_Formid = formMapper.Select_Formid(max(count-7,0),7);
        List<Integer> data_totaltime = formMapper.Select_Totaltime(max(count-7,0),7);
        List<Integer> data_Income = formMapper.Select_Income(max(count-7,0),7);
        Map<String,Object> res = new HashMap<>();
        res.put("avg",avg);
        res.put("Income",Income);
        res.put("data_Formid",data_Formid);
        res.put("data_totaltime",data_totaltime);
        res.put("data_Income",data_Income);
        CommonResp< Map<String,Object> > commonResp = new CommonResp<>();
        commonResp.setMessage("查询成功。");
        commonResp.setContent(res);
        commonResp.setSuccess(true);
        return commonResp;
    }

    public CommonResp MealTime(){
        List<Integer> data = new ArrayList<>();
        for(int i = 6;i<=24;i+=2){
            data.add(formMapper.SelectCount_MealTime(i));
        }
        CommonResp<List> commonResp = new CommonResp<>();
        commonResp.setSuccess(true);
        commonResp.setContent(data);
        commonResp.setMessage("查询成功。");
        return commonResp;
    }

    public CommonResp Work(){
        List<Integer> data = new ArrayList<>();
        for(int i = 7;i>0;i--){
            data.add(formMapper.SelectCount_Work(i));
        }
        CommonResp<List> commonResp = new CommonResp<>();
        commonResp.setSuccess(true);
        commonResp.setContent(data);
        commonResp.setMessage("查询成功。");
        return commonResp;
    }
    /*********************** 管理端 *********************************/
}
